define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/goods/index',
                    add_url: 'goods/goods/add',
                    edit_url: 'goods/goods/edit',
                    del_url: 'goods/goods/del',
                    multi_url: 'goods/goods/multi',
                    table: 'goods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                showToggle: false,
                showColumns: false,
                showExport: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate:false},
                        {field: 'name', title: __('Name'),operate:'LIKE'},
                        {field: 'image', title: __('Image'), formatter: Table.api.formatter.image},

                        {field: 'mprice', title: __('Mprice'), operate:false},
                        {field: 'gprice', title: __('Gprice'), operate:false},
                        {field: 'pprice', title: __('Pprice'), operate:false},
                        {field: 'gtype.title', title: __('商品类型'),operate:'LIKE'},
                        {field: 'images', title: __('Images'), formatter: Table.api.formatter.images},
                        {field: 'pdata', title: __('Pdata'), searchList: {"1":__('Pdata 1'),"2":__('Pdata 2')}, formatter: Table.api.formatter.normal},
                        {field: 'num', title: __('Num'),formatter:function (value) {
                                if(value < 1){
                                    return '售罄';
                                }else{
                                    return value;
                                }
                            },operate:false},
                        {field: 'volume', title: __('Volume'),operate:false},
                        // {field: 'longitude', title: __('Longitude'),operate:false},
                        // {field: 'latitude', title: __('Latitude'),operate:false},
                        // {field: 'address', title: __('Address'),operate:false},
                        {field: 'weigh', title: __('Weigh'),operate:false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'id', title: __('Id')},
                        // {field: 'name', title: __('Name')},
                        // {field: 'mprice', title: __('Mprice'), operate:'BETWEEN'},
                        // {field: 'gprice', title: __('Gprice'), operate:'BETWEEN'},
                        // {field: 'pprice', title: __('Pprice'), operate:'BETWEEN'},
                        // {field: 'image', title: __('Image'), formatter: Table.api.formatter.image},
                        // {field: 'images', title: __('Images'), formatter: Table.api.formatter.images},
                        // {field: 'gtype_id', title: __('Gtype_id')},
                        // {field: 'pdata', title: __('Pdata'), searchList: {"1":__('Pdata 1'),"2":__('Pdata 2')}, formatter: Table.api.formatter.normal},
                        // {field: 'longitude', title: __('Longitude')},
                        // {field: 'latitude', title: __('Latitude')},
                        // {field: 'address', title: __('Address')},
                        // {field: 'num', title: __('Num')},
                        // {field: 'volume', title: __('Volume')},
                        // {field: 'weigh', title: __('Weigh')},
                        // {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'gtype.id', title: __('Gtype.id')},
                        // {field: 'gtype.title', title: __('Gtype.title')},
                        // {field: 'gtype.image', title: __('Gtype.image'), formatter: Table.api.formatter.image},
                        // {field: 'gtype.createtime', title: __('Gtype.createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $('#choose').on('click',function () {
                location.href='/admin/goods/goods/choose';
            })
            Controller.api.bindevent();
        },
        edit: function () {
            var id = Config.id;
            $('#choose').on('click',function () {
                location.href='/admin/goods/goods/editAddress/id/'+ id;
            })
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});